package com.liu.nb.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.nb.entity.Circuitry;
import com.liu.nb.entity.Pole;
import com.liu.nb.mapper.PoleMapper;
import com.liu.nb.service.ICircuitryPoleService;
import com.liu.nb.service.ICircuitryService;
import com.liu.nb.service.IPoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 塔杆表 服务实现类
 * </p>
 *
 * @author liuziyi
 * @since 2021-08-31
 */
@Service
public class PoleServiceImpl extends ServiceImpl<PoleMapper, Pole> implements IPoleService {
    @Autowired
    ICircuitryService circuitryService;
    @Autowired
    ICircuitryPoleService circuitryPoleService;
    @Override
    public Page<Pole> getPoleInfo(HashMap<String, Object> map, int current, int size) {
        /*查询条件*/
        QueryWrapper<Pole> wrapper = new QueryWrapper<>();
        wrapper.inSql(StrUtil.isNotBlank(map.get("circuity").toString()), "id",
                "SELECT pole_id FROM circuitry_pole WHERE circuity_id LIKE '%"+map.get("circuity")+"%'")
                .eq(StrUtil.isNotBlank(map.get("state").toString()), "state", map.get("state"));
        /*查询结果*/
        Page<Pole> page = this.page(new Page<>(current, size), wrapper);
        page.getRecords().forEach(i->{
            List<Circuitry> id = circuitryService.list(new QueryWrapper<Circuitry>().inSql("id", "SELECT circuity_id FROM circuitry_pole WHERE pole_id ='" + i.getId() + "'"));
            String s = id.stream().map(c -> c.getName()).collect(Collectors.joining(","));
            i.setCName(s);
        });
        return page;
    }
}
